<?php
namespace Home\Controller;
use Think\Controller;
class TextController extends Controller {

    public $dsn  = "sqlsrv:Server=192.168.200.225;Database=text";
    public $user = "SA";
    public $pwd  = "123456";

    // public $dsn  = "sqlsrv:Server=127.0.0.1,1444;Database=text";
    // public $user = "SA";
    // public $pwd  = "Fgl88322";
                
    public function install(){

        // Chinese_PRC_CS_AI_WS  中文字符集
        $db = new \PDO( $this->dsn , $this->user , $this->pwd );
        $db->setAttribute(\PDO::ATTR_ERRMODE, \PDO::ERRMODE_EXCEPTION); 
        //$db->setAttribute(\PDO::ATTR_EMULATE_PREPARES, false);

        

        /* 开始一个事务，关闭自动提交 */
        $db->beginTransaction();
        try {
            //$db->exec("SET IDENTITY_INSERT table1 ON");
            
            /*  更改数据库架构及数据 */
            
            $sth = $db->exec("INSERT INTO [dbo].[table2] ([name]) VALUES ('xx');");
            $sth = $db->exec("INSERT INTO [dbo].[table1] ([name]) VALUES ('23');");
            
            //$db->exec("SET IDENTITY_INSERT table1 OFF");
        } catch (\PDOException $e) {
            print "Error: " . $e->getMessage() . "<br/>";
            /*  识别出错误并回滚更改 */
            $db->rollBack();
            die("SET IDENTITY_INSERT {$tableName} ON;".$sql);
        }
        $db->commit();

    }

}